package com.jianyun.jyzs.http;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jianyun.jyzs.bean.IntegratedResult;

import java.util.ArrayList;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by mlli on 2017/11/17.
 */

public class IShttpHelper {

    public interface IsOptionListener{
        void option(List<IntegratedResult> list);
    }

    public static void getIsOption(String method, String id, final IsOptionListener listener){
        Api api = RetrofitHelper.builderErpRoot().create(Api.class);
        api.getIntegrated(method,id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new JsonCallback<Object>() {
                    @Override
                    protected void onResponse(Object response) {
                        RceLog.d("lml",response.toString());
                        JsonArray jsonArray = new JsonParser().parse(response.toString()).getAsJsonArray();
                        RceLog.d("lml",jsonArray.toString());
                        List<IntegratedResult> list = new ArrayList<>();
                        Gson gson = new Gson();
                        for (JsonElement arr:jsonArray) {
                            IntegratedResult integratedResult = gson.fromJson(arr, IntegratedResult.class);
                            list.add(integratedResult);
                        }
                        listener.option(list);
                    }

                    @Override
                    protected void onException(String msg) {
                        RceLog.d("lml","msg"+msg);
                    }
                });

    }
}
